package com.example.dangdangbackend.service.impl;

import com.example.dangdangbackend.DTO.AddCategoryDTO;
import com.example.dangdangbackend.VO.CategoryFirstVO;
import com.example.dangdangbackend.VO.CategorySecondVO;
import com.example.dangdangbackend.VO.CategoryVO;
import com.example.dangdangbackend.common.CustomException;
import com.example.dangdangbackend.entity.CategoryFirst;
import com.example.dangdangbackend.entity.CategorySecond;
import com.example.dangdangbackend.mapper.CategoryMapper;
import com.example.dangdangbackend.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<String> selectFirst() {
        return categoryMapper.selectFirst();
    }
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<CategoryFirst> selectAllCategory() {
        return categoryMapper.selectAllCategoryFirstWithSecond();
    }

    @Override
    public void addCategoryFirst(String categoryFirstName) {
        if (categoryFirstName==null){
            throw new CustomException("标题名不能为空");
        }

        if (ifFirstNameExist(categoryFirstName)) {
            throw new CustomException("一级标题重复");
        }

        CategoryFirst categoryFirst = new CategoryFirst();
        //进行赋值操作
        categoryFirst.setCategoryFirstName(categoryFirstName);
        String currentTimeString = LocalDateTime.now().format(formatter);

        LocalDateTime time = LocalDateTime.parse(currentTimeString, formatter);
        categoryFirst.setCategoryFirstCreateTime(time);
        categoryFirst.setCategoryFirstUpdateTime(time);
        //进行添加操作
        categoryMapper.addFirstTittle(categoryFirst);
    }

    @Override
    public void addCategorySecond(AddCategoryDTO addCategoryDTO) {
        //所用的为前端数据流 首先进行赋值
        if(addCategoryDTO==null){
            throw new CustomException("二级标题为空");
        }

        if(ifSecondNameExist(addCategoryDTO.getCategorySecondName())){
            throw new CustomException("二级标题重复");
        }

        CategorySecond categorySecond = new CategorySecond();
        categorySecond.setCategorySecondName(addCategoryDTO.getCategorySecondName());
        categorySecond.setCategoryFirstId(addCategoryDTO.getCategoryFirstId());
        String currentTimeString = LocalDateTime.now().format(formatter);
        LocalDateTime time = LocalDateTime.parse(currentTimeString, formatter);
        categorySecond.setCategorySecondCreateTime(time);
        categorySecond.setCategorySecondUpdateTime(time);

        //更新操作
        categoryMapper.addSecondTittle(categorySecond);
    }

    /**
     * 检查一级标题是否存在
     * @param categoryFirstName
     * @return
     */
    @Override
    public boolean ifFirstNameExist(String categoryFirstName) {
        int count = categoryMapper.selectFirstName(categoryFirstName);
        return count > 0;
    }

    /**
     * 检查二级标题是否存在
     * @param categorySecondName
     * @return
     */
    @Override
    public boolean ifSecondNameExist(String categorySecondName) {
       int count = categoryMapper.selectSecondName(categorySecondName);
       return count>0;
    }

    /**
     * 获取所有的一级标题
     * @return
     */
    @Override
    public List<CategoryFirstVO> selectAllFirst() {
        return categoryMapper.selectAllFirst();
    }

    /**
     * 获取所有的二级标题
     * @return
     */
    @Override
    public List<CategorySecondVO> selectAllSecond() {
        return categoryMapper.selectAllSecond();
    }

    /**
     * 分页查询出所有的分类消息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<CategoryVO> selectByPage(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryVO> list = categoryMapper.selectByPage();

        PageInfo<CategoryVO> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    /**
     * 查询所有的一级标题
     * @param categoryId
     */
    @Override
    public void deleteFirst(int categoryId) {
        //先查询当前一级标题下的二级标题数量
        int count = categoryMapper.selectCount(categoryId);
        if(count>0){
            throw new CustomException("存在二级标题,无法直接删除");
        }

        categoryMapper.deleteFirst(categoryId);
    }

    /**
     * 删除的二级标题
     * @param categoryId
     */
    @Override
    public void deleteSecond(int categoryId) {

        //直接进行删除
        categoryMapper.deleteSecond(categoryId);
    }

    /**
     * 查询
     * @param categoryId
     * @return
     */
    @Override
    public int selectCount(int categoryId) {

        return categoryMapper.selectCount(categoryId);
    }

}
